﻿using System;
using System.Linq;
using SmartMusicBox.ServiceModel;
using SmartMusicBox.Framework.Parameter;
using SmartMusicBox.Framework.Helper;
using System.Windows.Media.Imaging;
using System.Windows.Media;

namespace SmartMusicBox.Logic
{
    public class SongLogic
    {
        #region api url
        readonly private static string PrefixPostURL = "http://douban.fm/j/mine/playlist?";
        #endregion

        public static void SendRequestForGetSongs(string channelNo , 
            Action<SongModel[]> OnReceived, Action<string> OnFaild)
        {
            var postURL = GetPostURL(channelNo);
            var request = WebRequestHelper.SendStringRequest(postURL, (data) =>
            {
                var result = SerializeHelper.ConvertJsonStringToObject<SongListResultModel>(data);
                OnReceived(result.SongList.Where(p => p.Subtype.ToLower() != "t").ToArray());

            }, OnFaild);              
        }

        public static string GetSongInfo(SongModel song)
        {
            return string.Join("\n", new string[] { 
                                string.Format("歌手:{0}" , song.Artist),
                                string.Format("歌名:{0}" , song.Title),
                                string.Format("专辑:{0}" , song.Albumtitle)
                            });
        }

        public static ImageSource GetImageSourceByUrl(string url)
        {
            return new BitmapImage(new Uri(url, UriKind.Absolute));
        }

        static private string GetPostURL(string channelNo)
        {
            var parameters = new ParameterCollectionModel();
            parameters.Add(new ParameterModel("type", "n"));
            parameters.Add(new ParameterModel("h", ""));
            parameters.Add(new ParameterModel("r", RandomHelper.RandomInstance.Next().ToString()));
            parameters.Add(new ParameterModel("channel", channelNo.ToString()));

            return string.Concat(PrefixPostURL, parameters.ToString());
        }
    }
}
